package com.ydd.moudle.guide;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;

import com.ydd.R;
import com.ydd.adapter.GuideViewPagerAdapter;
import com.ydd.base.BaseActivity;
import com.ydd.moudle.main.HomeActivity;

/**
 * Created by tao on 17-3-19.
 * 引导界面
 */

public class GuideActivity extends BaseActivity {
    public static final int PAGE_NUMS = 5;

    /**
     * UI
     */
    private ViewPager mViewPager;
    private GuideViewPagerAdapter mGuideAdapter;

    private boolean mIsOpaque = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_layout);

        initView();
    }

    private void initView() {
        changeStatusBarColor(R.color.transparent);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mGuideAdapter = new GuideViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mGuideAdapter);
        //为viewpager的滑动添加自定义的动画效果
        mViewPager.setPageTransformer(true, new CrossfadePageTransformer());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == PAGE_NUMS - 2 && positionOffset > 0) {
                    if (isOpaque()) {
                        mViewPager.setBackgroundColor(Color.TRANSPARENT);
                        setIsOpaque(false);
                    } else {
                        mViewPager.setBackgroundColor(getResources().getColor(R.color.primary_material_light));
                        setIsOpaque(true);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                if (position == PAGE_NUMS - 2) {
                } else if (position < PAGE_NUMS - 2) {
                } else if (position == PAGE_NUMS - 1) {
                    endTutorial();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void endTutorial() {
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

    public boolean isOpaque() {
        return mIsOpaque;
    }

    public void setIsOpaque(boolean opaque) {
        mIsOpaque = opaque;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mViewPager != null) {
            mViewPager.clearOnPageChangeListeners();
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
